# 棋盘大小
BOARD_SIZE = 15


# 初始化棋盘
def init_board():
    return [[' ' for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]


# 打印棋盘
def print_board(board):
    print('  ', end='')
    for i in range(BOARD_SIZE):
        print(f'{i:2}', end='')
    print()
    for i in range(BOARD_SIZE):
        print(f'{i:2}', end='')
        for j in range(BOARD_SIZE):
            print(f'{board[i][j]:2}', end='')
        print()


# 检查是否有五子连成一线
def check_win(board, x, y, player):
    directions = [(1, 0), (0, 1), (1, 1), (1, -1)]
    for dx, dy in directions:
        count = 1
        # 正向检查
        for i in range(1, 5):
            new_x = x + i * dx
            new_y = y + i * dy
            if 0 <= new_x < BOARD_SIZE and 0 <= new_y < BOARD_SIZE and board[new_x][new_y] == player:
                count += 1
            else:
                break
        # 反向检查
        for i in range(1, 5):
            new_x = x - i * dx
            new_y = y - i * dy
            if 0 <= new_x < BOARD_SIZE and 0 <= new_y < BOARD_SIZE and board[new_x][new_y] == player:
                count += 1
            else:
                break
        if count >= 5:
            return True
    return False


# 主游戏循环
def main():
    board = init_board()
    current_player = 'X'
    while True:
        print_board(board)
        try:
            x, y = map(int, input(f"玩家 {current_player} 的回合，请输入落子坐标 (x y): ").split())
            if 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE and board[x][y] == ' ':
                board[x][y] = current_player
                if check_win(board, x, y, current_player):
                    print_board(board)
                    print(f"玩家 {current_player} 获胜！")
                    break
                current_player = 'O' if current_player == 'X' else 'X'
            else:
                print("无效的落子位置，请重新输入。")
        except ValueError:
            print("输入无效，请输入两个整数作为坐标。")


if __name__ == "__main__":
    main()
